////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGAInput.cpp
//
////////////////////////////////////////
#include "SAGAInput.h"
////////////////////////////////////////
//	Function Name: SAGAInput
//	Purpose:
//		Constructor
SAGAInput::SAGAInput(HINSTANCE hInst) 
{
   m_hInst   = hInst;
   m_pDevice = NULL;
   m_hDLL    = NULL;
}
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: ~SAGAInput
//	Purpose:
//		Destructor: Just call the Release method
SAGAInput::~SAGAInput() 
{ 
   Release(); 
}
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Create Device
//	Purpose:
//		Create the dll objects. This functions loads the appropriate dll.
HRESULT SAGAInput::CreateDevice() 
{
   // load the DLL containing interface implementation
   m_hDLL = LoadLibraryEx("SAGADI.dll",NULL,0);
   if(!m_hDLL) 
   {
      MessageBox(NULL,
         "Loading SAGADI.dll from lib failed.",
         "SAGAEngine - error", MB_OK | MB_ICONERROR);
      return E_FAIL;
    }
   
   CREATEINPUTDEVICE _CreateInputDevice = 0;
   HRESULT hr;
   
   // function pointer to dll's 'CreateInputDevice' function
   _CreateInputDevice = (CREATEINPUTDEVICE)
                          GetProcAddress(m_hDLL,
                                 "CreateInputDevice");
   // call dll's create function
   hr = _CreateInputDevice(m_hDLL, &m_pDevice);
   if(FAILED(hr)){
      MessageBox(NULL,
         "CreateInputDevice() from lib failed.",
         "SAGAEngine - error", MB_OK | MB_ICONERROR);
      m_pDevice = NULL;
      return E_FAIL;
      }
   
   return S_OK;
} 
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Release
//	Purpose:
//		Cleanup the dll objects.
void SAGAInput::Release() 
{
   RELEASEINPUTDEVICE _ReleaseInputDevice = 0;
   HRESULT hr;
   
   if (m_hDLL) 
   {
      // function pointer to dll 'ReleaseInputDevice' function
      _ReleaseInputDevice = (RELEASEINPUTDEVICE)
         GetProcAddress(m_hDLL, "ReleaseInputDevice");
   }
   // call dll's release function
   if (m_pDevice) 
   {
      hr = _ReleaseInputDevice(&m_pDevice);
      if(FAILED(hr)) m_pDevice = NULL;
   } 
}
//
////////////////////////////////////////